import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import Adam

x_train = np.random.random((1000, 784))
y_train = np.random.randint(2, size = (1000, 1))
x_test = np.random.random((200, 784))
y_test = np.random.randint(2, size = (200, 1))

# 构造采用序列的模型
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=784))
model.add(Dense(1, activation='sigmoid'))   # 这里可以add多个层，也能合并两个模型

# 编译模型
model.compile(optimizer='Adam', loss = 'binary_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=10, verbose = 2, batch_size = 32)
# epochs 代表训练次数; verbose表示训练时显示实时信息; batch_size表示梯度下降时每个batch包含的样本数

# 测试模型
score = model.evaluate(x_test, y_test, batch_size = 32)

# Epoch 1/10
# 32/32 - 0s - loss: 0.7144 - accuracy: 0.5120
# Epoch 2/10
# 32/32 - 0s - loss: 0.6881 - accuracy: 0.5450
# Epoch 3/10
# 32/32 - 0s - loss: 0.6865 - accuracy: 0.5460
# Epoch 4/10
# 32/32 - 0s - loss: 0.6726 - accuracy: 0.5700
# Epoch 5/10
# 32/32 - 0s - loss: 0.6594 - accuracy: 0.6040
# Epoch 6/10
# 32/32 - 0s - loss: 0.6462 - accuracy: 0.6670
# Epoch 7/10
# 32/32 - 0s - loss: 0.6294 - accuracy: 0.6710
# Epoch 8/10
# 32/32 - 0s - loss: 0.6159 - accuracy: 0.6860
# Epoch 9/10
# 32/32 - 0s - loss: 0.6039 - accuracy: 0.7250
# Epoch 10/10
# 32/32 - 0s - loss: 0.5771 - accuracy: 0.7400
# 7/7 [==============================] - 0s 570us/step - loss: 0.7143 - accuracy: 0.5200